package cn.buk.api.szkj.service;

import cn.buk.api.szkj.dto.eterm.request.*;
import cn.buk.api.szkj.dto.eterm.response.*;
import cn.buk.common.util.EncryptUtil;
import cn.buk.common.util.HttpUtil;
import cn.buk.common.util.JsonUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.hc.core5.http.NameValuePair;

import java.util.List;


/**
 * @author yfdai
 */
public class SzkjEtermServiceImpl extends AbstractServiceImpl implements SzkjEtermService {

  public SzkjEtermServiceImpl(String username, String apiUrl) {
    super(username, apiUrl);
  }


  private <T extends BaseResponse> T execApiRequest(final String functionName, EtermRequest rq, Class<T> clazz) {
    final String requestStr = JsonUtil.toJSONString(rq);
    List<NameValuePair> params = prepareParams(functionName, this.username, requestStr);

    final String responseStr = EncryptUtil.decodeUnicode(HttpUtil.postUrl(this.apiUrl, params));
    if (this.debugMode) {
      System.out.println("response: " + responseStr);
    }

    T response = null;

    if (StringUtils.trimToEmpty(responseStr).length() == 0) {
      try {
        response =  clazz.newInstance();
      } catch (InstantiationException | IllegalAccessException e) {
        e.printStackTrace();
        return null;
      }
      response.setCode(-10000);
      response.setMsg("接口返回空");
    } else {
      try {
        response = JsonUtil.convertJson2Obj(responseStr, clazz);
      } catch (RuntimeException ex) {
        ex.printStackTrace();
      } finally {
        if (response == null) {
          try {
            response = clazz.newInstance();
          } catch (Exception e) {
            return null;
          }
          response.setCode(-10000);
        }
      }
    }

    response.setRequestStr(requestStr);
    response.setResponseStr(responseStr);
    response.setQueryId(rq.getUserInfo().getQueryId());

    return response;
  }

  @Override
  public CmdResponse executeEtermCmd(String etermUsername, String etermPassword, int queryId, String cmd) {
    CmdRequest cmdRequest = new CmdRequest();
    cmdRequest.setEtermCmd(cmd);

    EtermRequest<CmdRequest> request = new EtermRequest<>();
    request.setUserInfo(prepareUserInfo(etermUsername, etermPassword, queryId));
    request.setSendData(cmdRequest);

    return execApiRequest("eterm_raw", request, CmdResponse.class);
  }



  @Override
  public CreatePnrResponse createPnr(String etermUsername, String etermPassword, int queryId, PnrCreateRequest createPnrRequest) {
    EtermRequest<PnrCreateRequest> request = new EtermRequest<>();
    request.setUserInfo(prepareUserInfo(etermUsername, etermPassword, queryId));
    request.setSendData(createPnrRequest);

    return execApiRequest("eterm_create_pnr", request, CreatePnrResponse.class);
  }

  @Override
  public RtpnrResponse rtpnr(String etermUsername, String etermPassword, int queryId, PnrRtRequest rtRequest) {
    EtermRequest<PnrRtRequest> request = new EtermRequest<>();
    request.setUserInfo(prepareUserInfo(etermUsername, etermPassword, queryId));
    request.setSendData(rtRequest);

    return execApiRequest("eterm_rtpnr", request, RtpnrResponse.class);
  }

  @Override
  public PnrPatResponse patPnr(String etermUsername, String etermPassword, int queryId, PnrPatRequest patRequest) {
    EtermRequest<PnrPatRequest> request = new EtermRequest<>();
    request.setUserInfo(prepareUserInfo(etermUsername, etermPassword, queryId));
    request.setSendData(patRequest);

    return execApiRequest("eterm_rtpnr_pat_sfc", request, PnrPatResponse.class);
  }

  @Override
  public CmdResponse xepnr(String etermUsername, String etermPassword, int queryId, PnrXeRequest xeRequest) {
    EtermRequest<PnrXeRequest> request = new EtermRequest<>();
    request.setUserInfo(prepareUserInfo(etermUsername, etermPassword, queryId));
    request.setSendData(xeRequest);

    return execApiRequest("eterm_xepnr", request, CmdResponse.class);
  }


  @Override
  public EtdzResponse etdz(String etermUsername, String etermPassword, int queryId, EtdzRequest etdzRequest) {
    EtermRequest<EtdzRequest> request = new EtermRequest<>();
    request.setUserInfo(prepareUserInfo(etermUsername, etermPassword, queryId));
    request.setSendData(etdzRequest);

    return execApiRequest("eterm_auto_ticket", request, EtdzResponse.class);
  }



  @Override
  public DetrResponse detr(String etermUsername, String etermPassword, int queryId, DetrRequest detrRq) {
    EtermRequest<DetrRequest> request = new EtermRequest<>();
    request.setUserInfo(prepareUserInfo(etermUsername, etermPassword, queryId));
    request.setSendData(detrRq);

    return execApiRequest("eterm_detr", request, DetrResponse.class);
  }

  @Override
  public TslResponse tsl(String etermUsername, String etermPassword, int queryId, TslRequest detrRq) {
    EtermRequest<TslRequest> request = new EtermRequest<>();
    request.setUserInfo(prepareUserInfo(etermUsername, etermPassword, queryId));
    request.setSendData(detrRq);
    return execApiRequest("eterm_tpr", request, TslResponse.class);
  }

  @Override
  public QtResponse qt(String etermUsername, String etermPassword, int queryId, QtRequest qtRequest) {
    EtermRequest<QtRequest> request = new EtermRequest<>();
    request.setUserInfo(prepareUserInfo(etermUsername, etermPassword, queryId));
    request.setSendData(qtRequest);
    return execApiRequest("eterm_qt", request, QtResponse.class);
  }

  @Override
  public QnResponse qn(String etermUsername, String etermPassword, int queryId, QnRequest qtRequest) {
    EtermRequest<QnRequest> request = new EtermRequest<>();
    request.setUserInfo(prepareUserInfo(etermUsername, etermPassword, queryId));
    request.setSendData(qtRequest);
    return execApiRequest("eterm_flight_delay", request, QnResponse.class);
  }

  @Override
  public FfResponse ff(String etermUsername, String etermPassword, int queryId, FfRequest ffRequest) {
    EtermRequest<FfRequest> request = new EtermRequest<>();
    request.setUserInfo(prepareUserInfo(etermUsername, etermPassword, queryId));
    request.setSendData(ffRequest);
    return execApiRequest("eterm_stopover", request, FfResponse.class);
  }

  @Override
  public FdResponse fd(String etermUsername, String etermPassword, int queryId, FdRequest fdRequest) {
    EtermRequest<FdRequest> request = new EtermRequest<>();
    request.setUserInfo(prepareUserInfo(etermUsername, etermPassword, queryId));
    request.setSendData(fdRequest);
    return execApiRequest("eterm_fd", request, FdResponse.class);

  }

}
